<template>
  <div class="app-container">
    <el-form :model="queryParam" ref="queryForm" :inline="true" :rules="rules">
      <el-form-item label="试卷Id：" prop="paperId">
        <el-input v-model="queryParam.paperId" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户Id：" prop="userId">
        <el-input v-model="queryParam.userId" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户名称：" >
        <el-input v-model="queryParam.userName" clearable></el-input>
      </el-form-item>
      <el-form-item label="试卷类型：" >
        <el-select v-model="queryParam.paperTypeId" placeholder="试卷类型" clearable>
          <el-option v-for="item in paperTypeEnum" :key="item.id" :value="item.id" :label="item.categoryName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学科：" >
        <el-select v-model="queryParam.subjectId"  clearable>
          <el-option v-for="item in subjectAll" :key="item.id" :value="item.id" :label="item.categoryName" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="tableData" border fit highlight-current-row style="width: 100%">
      <el-table-column prop="paperId" label="试卷Id"  width="100" />
      <el-table-column prop="userId" label="用户Id"/>
      <el-table-column prop="userName" label="用户名称"/>
      <el-table-column prop="subjectName" label="学科名称"/>
      <el-table-column prop="paperTypeName" label="试卷类型"/>
      <el-table-column prop="paperName" label="试卷名称"/>
      <el-table-column prop="score" label="得分"/>
      <el-table-column prop="totalScore" label="总分"/>
      <el-table-column prop="time" label="提交时间" width="100px"/>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParam.pageIndex" :limit.sync="queryParam.pageSize"
                @pagination="search"/>
  </div>
</template>

<script>

import { mapGetters, mapState, mapActions } from 'vuex'
import Pagination from '@/components/Pagination'
import examPaperAnswerApi from '@/api/examPaperAnwser'
import examPaperApi from '@/api/examPaper'

export default {
  components: { Pagination },
  data () {
    return {
      queryParam: {
        paperId: null,
        userId: null,
        subjectId: null,
        paperTypeId: null,
        userName: null,
        pageIndex: 1,
        pageSize: 10
      },
      listLoading: false,
      tableData: [],
      total: 0,
      paperTypeEnum: [],
      subjectAll: [],
      rules: {
        paperId: [
          { type: 'string', message: '试卷Id必须是数字', trigger: 'blur' },
          { validator: this.validateSendUserId, trigger: 'blur' }
        ],
        userId: [
          { type: 'string', message: '用户Id必须是数字', trigger: 'blur' },
          { validator: this.validateSendUserId, trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.initSubject()
    this.search()
    this.subjectAllsearch()
    this.paperTypesearch()
  },
  methods: {
    validateUserId (rule, value, callback) {
      if (value === null || value === '') {
        callback()
      } else {
        const numValue = Number(value)
        console.log(value)
        if (isNaN(numValue)) {
          callback(new Error('用户Id必须是数字'))
        } else if (numValue <= 0) {
          callback(new Error('用户Id必须大于0'))
        } else {
          callback()
        }
      }
    },
    validateSendUserId (rule, value, callback) {
      if (value === null || value === '') {
        callback()
      } else {
        const numValue = Number(value)
        if (isNaN(numValue)) {
          callback(new Error('试卷Id必须是数字'))
        } else if (numValue <= 0) {
          callback(new Error('试卷Id必须大于0'))
        } else {
          callback()
        }
      }
    },
    subjectAllsearch () {
      examPaperApi.subjectList().then(data => {
        const re = data.response
        this.subjectAll = re
        // console.log(re)
      })
    },
    paperTypesearch () {
      examPaperApi.paperType().then(data => {
        const re = data.response
        this.paperTypeEnum = re
      })
    },
    search () {
      this.listLoading = true
      examPaperAnswerApi.page(this.queryParam).then(data => {
        const re = data.response
        this.tableData = re.list
        this.total = re.total
        this.queryParam.pageIndex = re.pageNum
        this.listLoading = false
      })
    },
    submitForm () {
      this.queryParam.pageIndex = 1
      this.$refs.queryForm.validate((valid) => {
        if (valid) {
          this.search()
        } else {
          return false
        }
      })
    },
    ...mapActions('exam', { initSubject: 'initSubject' })
  },
  computed: {
    ...mapGetters('enumItem', ['enumFormat']),
    ...mapGetters('exam', ['subjectEnumFormat']),
    ...mapState('exam', { subjects: state => state.subjects })
  }
}
</script>
